import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

// 中文内容提取
public class Main {
    // 使用LinkedHashMap保持插入顺序，HashSet用于快速去重
    private static final LinkedHashMap<String, String> uniqueStrings = new LinkedHashMap<>();
    private static final Set<String> existingContents = new HashSet<>();

    public static void main(String[] args) {
        Path rootPath = Paths.get(AddInfo.filePath);


        try {
            Files.walk(rootPath)
                    .filter(Files::isRegularFile)
                    .filter(path -> path.toString().endsWith(".kt"))
                    .filter(p -> {
                        String fileName = p.toString().toLowerCase();
                        return fileName.endsWith(".xml") || fileName.endsWith(".kt");
                    })
                    .filter(path -> !path.getFileName().toString().equalsIgnoreCase("strings.xml"))
                    .filter(path -> !path.getFileName().toString().equalsIgnoreCase("themes.xml"))
                    .forEach(Main::processFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 处理完成后输出所有唯一键值对
//        System.out.println("\n去重后的键值对：");
//        int index = 1;
//        for (Map.Entry<String, String> entry : uniqueStrings.entrySet()) {
//            System.out.printf("<string name=\"%s\">%s</string>\n", entry.getValue(), entry.getKey());
//        }
    }

    private static void processFile(Path filePath) {

        try {
            String content = Files.readString(filePath, StandardCharsets.UTF_8);
            // 去除单行注释和多行注释的正则表达式
            Pattern COMMENT_PATTERN = Pattern.compile("//.*?$|/\\*.*?\\*/", Pattern.MULTILINE | Pattern.DOTALL);
            String withoutComments = COMMENT_PATTERN.matcher(content).replaceAll("");//去除所有注释

            Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+");//获取中文正则表达式
            Matcher matcher = pattern.matcher(withoutComments);//获取中文内容

            boolean found = false;
            while (matcher.find()) {
                String quotedContent = matcher.group();
                    if (!found) {
                        System.out.println("\n发现中文内容 [" + filePath + "]");
                        found = true;
                    }
                    System.out.println("-> 完整内容: " + quotedContent);

            }
        } catch (IOException e) {
            System.err.println("无法读取文件: " + filePath);
        }
    }

}